﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Presentation.Web.Admin.Controllers
{
    [HandleError]
    public class ExportController : Infrastructure.Crosscutting.NetFramework.Controllers.BaseController
    {
        Application.Admin.Report.IReportService reportService;

        Application.Admin.MobileClient.IMobileClientService clientService;

        public ExportController(Infrastructure.Crosscutting.NetFramework.IWorkContext work,
             Application.Admin.Report.IReportService report, Application.Admin.MobileClient.IMobileClientService client)
            : base(work)
        {
            if (report == null)
            {
                throw new ArgumentNullException("report is null");
            }
            if (client == null)
            {
                throw new ArgumentNullException("client is null");
            }
            reportService = report;
            clientService = client;
        }

        public ActionResult HomeExportToWord()
        {
            var html = "";
            html += "<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\" align=\"left\" width=\"681\">";
            html += "  <tr  style=\"border:hidden\">";
            html += "    <td colspan=\"4\" valign=\"top\"><div align=\"center\"><strong>统计情况 </strong></div></td>";
            html += "  </tr>";
            html += "  <tr>";
            html += "    <td width=\"177\" valign=\"top\"><p align=\"center\"><strong>今日</strong><strong> </strong></p></td>";
            html += "    <td width=\"142\" valign=\"top\"><p align=\"center\"><strong>昨天</strong><strong> </strong></p></td>";
            html += "    <td width=\"161\" valign=\"top\"><p align=\"center\"><strong>最近一周</strong><strong> </strong></p></td>";
            html += "    <td width=\"202\" valign=\"top\"><p align=\"center\"><strong>最近一个月</strong><strong> </strong></p></td>";
            html += "  </tr>";
            html += "  <tr style = 'color:red;'>";
            html += "    <td width=\"177\" valign=\"top\"><p align=\"center\">" + reportService.GetSightSiginTodayReport() + "</p></td>";
            html += "    <td width=\"142\" valign=\"top\"><p align=\"center\">" + reportService.GetSightSiginYesterdayReport() + "</p></td>";
            html += "    <td width=\"161\" valign=\"top\"><p align=\"center\">" + reportService.GetSightSiginWeekReport() + "</p></td>";
            html += "    <td width=\"202\" valign=\"top\"><p align=\"center\">" + reportService.GetSightSiginMonthReport() + "</p></td>";
            html += "  </tr>";
            html += "  <tr>";
            html += "    <td width=\"177\" valign=\"top\"><p align=\"center\"><strong>今日签到数</strong><strong> </strong></p></td>";
            html += "    <td width=\"142\" valign=\"top\"><p align=\"center\"><strong>昨天签到数</strong><strong> </strong></p></td>";
            html += "    <td width=\"161\" valign=\"top\"><p align=\"center\"><strong>最近一周签到数</strong><strong> </strong></p></td>";
            html += "    <td width=\"202\" valign=\"top\"><p align=\"center\"><strong>最近一个月签到数</strong><strong> </strong></p></td>";
            html += "  </tr>";
            html += "  <tr style = 'color:red;'>";
            html += "    <td width=\"177\" valign=\"top\"><p align=\"center\">" + reportService.GetSightCommTodayReport() + "</p></td>";
            html += "    <td width=\"142\" valign=\"top\"><p align=\"center\">" + reportService.GetSightCommYesterdayReport() + "</p></td>";
            html += "    <td width=\"161\" valign=\"top\"><p align=\"center\">" + reportService.GetSightCommWeekReport() + "</p></td>";
            html += "    <td width=\"202\" valign=\"top\"><p align=\"center\">" + reportService.GetSightCommMonthReport() + "</p></td>";
            html += "  </tr>";
            html += "  <tr>";
            html += "    <td width=\"177\" valign=\"top\"><p align=\"center\"><strong>今日评论数</strong><strong> </strong></p></td>";
            html += "    <td width=\"142\" valign=\"top\"><p align=\"center\"><strong>昨天评论数</strong><strong> </strong></p></td>";
            html += "    <td width=\"161\" valign=\"top\"><p align=\"center\"><strong>最近一周评论数</strong><strong> </strong></p></td>";
            html += "    <td width=\"202\" valign=\"top\"><p align=\"center\"><strong>最近一个月评论数</strong><strong> </strong></p></td>";
            html += "  </tr>";
            html += "  <tr style = 'color:red;'>";
            html += "    <td width=\"177\" valign=\"top\"><p align=\"center\">" + reportService.GetNewClientTodayReport() + "</p></td>";
            html += "    <td width=\"142\" valign=\"top\"><p align=\"center\">" + reportService.GetNewClientYesterdayReport() + "</p></td>";
            html += "    <td width=\"161\" valign=\"top\"><p align=\"center\">" + reportService.GetNewClientWeekReport() + "</p></td>";
            html += "    <td width=\"202\" valign=\"top\"><p align=\"center\">" + reportService.GetNewClientMonthReport() + "</p></td>";
            html += "  </tr>";
            html += "  <tr>";
            html += "    <td width=\"177\" valign=\"top\"><p align=\"center\"><strong>今日新增</strong><strong> </strong><br />";
            html += "      <strong>客户端</strong><strong> </strong></p></td>";
            html += "    <td width=\"142\" valign=\"top\"><p align=\"center\"><strong>昨天新增</strong><strong> </strong><br />";
            html += "      <strong>客户端</strong><strong> </strong></p></td>";
            html += "    <td width=\"161\" valign=\"top\"><p align=\"center\"><strong>最近一周新增</strong><strong> </strong><br />";
            html += "      <strong>客户端</strong><strong> </strong></p></td>";
            html += "    <td width=\"202\" valign=\"top\"><p align=\"center\"><strong>最近一个月新增</strong><strong> </strong><br />";
            html += "      <strong>客户端</strong><strong> </strong></p></td>";
            html += "  </tr>";
            html += "  <tr>";
            html += "    <td colspan=\"4\" valign=\"top\"><div align=\"center\"><strong>客户端总数：" + this.GetClientCount() + "</strong></div></td>";
            html += "  </tr>";
            html += "</table>";
            return new Infrastructure.Crosscutting.NetFramework
                .Result.CustomWordResult(html, "imap客户端统计-" + System.DateTime.Now.ToFileTime(), "gb2312", System.Text.Encoding.Default);
        }

        protected int GetClientCount()
        {
            var res = clientService.GetList().Where(d => d.State == true)
                .Distinct().Count();
            return res;
        }
    }
}
